/*
 * test_http_s.cpp
 *
 *  Created on: Dec 11, 2013
 *      Author: killerg
 */





#include "share/http_s.h"
#include "share/json/json.h"
#include "share/util.h"
#include "share/clock.h"

static bool running = false;
static void handle_ctrl_c(int signal) {
    running = false;
}
static int reqnum = 0;
bool test_http_s(int fps) {
    http_server svr;

    if (!svr.init("0.0.0.0", 9999/*, "/home/killerg/temp/ssl/pkey", "/home/killerg/temp/ssl/cert"*/)) {
        perror("http_server::init error");
        return false;
    }

    on_signal(SIGINT, handle_ctrl_c);
    running = true;


    std::list<http_server::p_request> req_list;

    svr.handle("/index.html", [](http_server::p_request req){
    	req->send_reply(HTTP_OK, "hello world");
    	++reqnum;
    });

    svr.handle("/killerg/xixi", [&req_list](http_server::p_request req){
        const char* rep = fmt_cstr("from:%s, url:%s body:%s\n", req->ip.c_str(), req->path.c_str(), req->query["hello"].c_str());
        req->send_reply(HTTP_OK, evhttp_request_get_uri(req->req));
        ++reqnum;
    });

    svr.handle("/killerg/haha", [&req_list](http_server::p_request req){
        const char* rep = fmt_cstr("from:%s, url:%s body:%s\n", req->ip.c_str(), req->path.c_str(), req->query["world"].c_str());
        req->send_reply(HTTP_OK, evhttp_request_get_uri(req->req));
        ++reqnum;
    });

    my_clock_t clock;
    clock.init(fps);

    while (running) {
        clock.begin_update();
        svr.update();
        clock.end_update();
        clock.wait_frame();
        if (clock.sec_jump) {
            printf("%d\n", reqnum);
            reqnum = 0;
        }
        if (clock.eclipse > 20000) {
        	break;
        }
    }

    clock.destroy();
    svr.destroy();

    return true;
}



